package com.house365.core.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;

import com.house365.core.R;
import com.house365.core.action.ActionTag;
import com.house365.core.inter.ConfirmDialogListener;

/**
 * @author py
 * @date 2013-4-23
 * @comment
 */
public class DialogUtil {


	public static void showConfrimDialog(Context context, String msg,String positiveButtonText,
			final ConfirmDialogListener dialogListener) {
		showConfrimDialog(context, null, msg, positiveButtonText, context.getResources().getString(R.string.dialog_button_cancel), dialogListener);
		
	}

	public static void showConfrimDialog(Context context, int msg,int positiveButtonText,
			final ConfirmDialogListener dialogListener) {
		showConfrimDialog(context, -1, msg,positiveButtonText,R.string.dialog_button_cancel, dialogListener);

	}
	/**
	 * 确定对话框
	 * @param context
	 * @param title
	 * @param msg
	 * @param positiveButtonText
	 * @param negativeButtonText
	 * @param dialogListener
	 */
	public static void showConfrimDialog(Context context, String title, String msg,
			String positiveButtonText, String negativeButtonText,
			final ConfirmDialogListener dialogListener) {
		Builder builder = new AlertDialog.Builder(context);
		builder.setCancelable(false);
		if (!TextUtils.isEmpty(title)) {
			builder.setTitle(title);
		}
		if (!TextUtils.isEmpty(msg)) {
			builder.setMessage(msg);
		}
		if(!TextUtils.isEmpty(negativeButtonText)){
			builder.setNegativeButton(negativeButtonText,
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							if (dialogListener != null) {
								dialogListener.onNegative(dialog);
							}
						}
					});
		}
		if(!TextUtils.isEmpty(positiveButtonText)){
			builder.setPositiveButton(positiveButtonText,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							if (dialogListener != null) {
								dialogListener.onPositive(dialog);
							}
							dialog.dismiss();
						}
					});
		}
		builder.show();
	}
	/**
	 * 确定对话框
	 * @param context
	 * @param title
	 * @param msg
	 * @param positiveButtonText
	 * @param negativeButtonText
	 * @param dialogListener
	 */
	public static void showConfrimDialog(Context context, int title, int msg,
			int positiveButtonText, int negativeButtonText,
			final ConfirmDialogListener dialogListener) {
		Builder builder = new AlertDialog.Builder(context);
		builder.setCancelable(false);
		if (title > 0) {
			builder.setTitle(title);
		}
		if (msg > 0) {
			builder.setMessage(msg);
		}
		if(positiveButtonText>0){
			builder.setPositiveButton(positiveButtonText,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							if (dialogListener != null) {
								dialogListener.onPositive(dialog);
							}
							dialog.dismiss();
						}
					});
		}
		if(negativeButtonText>0){
		builder.setNegativeButton(negativeButtonText,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						if (dialogListener != null) {
							dialogListener.onNegative(dialog);
						}
					}
				});
		}
		builder.show();
	}

	/**
	 * 系统退出
	 * @param context
	 * @param appid
	 */
	public static void exitDialog(final Context context, int appid) {

		showConfrimDialog(context, appid, R.string.confirm_exit_info,
				R.string.dialog_button_cancel, R.string.dialog_button_exit,
				new ConfirmDialogListener() {

					@Override
					public void onPositive(DialogInterface dialog) {
						Intent intent = new Intent(
								ActionTag.INTENT_ACTION_LOGGED_OUT);
						context.sendBroadcast(intent);
						((Activity) context).getApplication().onTerminate();

					}

					@Override
					public void onNegative(DialogInterface dialog) {
						// TODO Auto-generated method stub

					}
				});
	}
	
}
